package com.pet.server.ext.service;


import com.pet.server.ext.ApiError;
import com.pet.server.ext.constants.BaseAttribute;
import com.pet.server.ext.data.ALiMaMa;
import com.pet.server.ext.data.Constant;
import com.pet.server.ext.dbutil.JdbcTemplate;
import com.pet.server.ext.dbutil.Sql;


public class ALiMaMaService {
	private JdbcTemplate jdbcTemplate = new JdbcTemplate();
	
	
	public void delteProductData(String petCode,String cateCode) throws ApiError{
		
		Sql sql = new Sql("delete img from `product_data` p,`image` img where p.`id`=img.`oid` and img.`type`=? and  p.`pet_cate`=? and p.`product_cate`=?");
		    sql.addParam(BaseAttribute.image_type.product_data.toString(),petCode,cateCode);
		    jdbcTemplate.updateObject(sql);
		    
            Sql sql1 = new Sql("delete p from `product_data` p where   p.`pet_cate`=? and p.`product_cate`=?");
            sql1.addParam(petCode,cateCode);
		    jdbcTemplate.updateObject(sql1);    
	}
	
	public ALiMaMa saveProductData(ALiMaMa u) throws ApiError{
		Sql sql = new Sql("INSERT INTO product_data (product_name,product_description,seller_id,source,ico_path,product_href,client_url,product_cate,pet_breeds,pet_cate,create_date,product_age,product_brand,product_price,sale_price,state,city,click_url,sort,num_iid) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
		    sql.addParam(u.product_name,u.product_description,u.seller_id,u.source,u.ico_path,u.product_href,u.client_url,u.product_cate,u.pet_breeds,u.pet_cate,u.create_date,u.product_age,u.product_brand,u.product_price,u.sale_price,u.state,u.city,u.click_url,u.sort,u.num_iid);
		    Integer id = jdbcTemplate.saveObject(sql);
		    u.id=id;
		    return u;
	}
	
	public Constant findALiMaMa(String conts_name) throws ApiError{
		Sql sql = new Sql("SELECT * FROM constant c WHERE c.conts_name=? limit 1");
		    sql.addParam(conts_name);
	    return  jdbcTemplate.findObject(sql, Constant.class);
	}
	
}
